#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
int a[N], b[N];
int c[N], d[N];
int cnt1[N], cnt2[N];
ll sum1[N], sum2[N];
int lowbit(int x) { return x & -x; }
void update(int cnt[], ll sum[], int x, int d, int y) {
  for (int i = x; i <= n; i += lowbit(i)) {
    cnt[i] += d;
    sum[i] += y;
  }
}
ll query_sum(ll sum[], int x) {
  ll res = 0;
  for (int i = x; i > 0; i -= lowbit(i)) res += sum[i];
  return res;
}
int query_cnt(int cnt[], int x) {
  int res = 0;
  for (int i = x; i > 0; i -= lowbit(i)) res += cnt[i];
  return res;
}
int pos[N];
void init() {
  int k = sqrt(1ll * n * n / m) + 1;
  rep(i, 1, n) pos[i] = (i - 1) / k + 1;
}
struct Node {
  int x, y, id;
  bool operator<(Node o) const {
    if (pos[x] == pos[o.x]) {
      if (pos[x] & 1)
        return y < o.y;
      else
        return y > o.y;
    } else
      return pos[x] < pos[o.x];
  }
} q[N];
ll ans[N];
int L, R;
ll sum, sumx, sumy;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) cin >> b[i];
  cin >> m;
  rep(i, 1, m) cin >> q[i].x >> q[i].y;
  rep(i, 1, m) q[i].id = i;

  rep(i, 1, n) c[i] = a[i];
  sort(c + 1, c + n + 1);
  rep(i, 1, n) a[i] = lower_bound(c + 1, c + n + 1, a[i]) - c;
  rep(i, 1, n) d[i] = b[i];
  sort(d + 1, d + n + 1);
  rep(i, 1, n) b[i] = lower_bound(d + 1, d + n + 1, b[i]) - d;
  init();
  sort(q + 1, q + m + 1);
  rep(i, 1, m) {
    while (L < q[i].x) {
      ++L;
      // add a[L]
      // sum += cnt[b[j] < L] * a[L] - sum[b[j] < L]
      // sum += sum[b[j] > L] - cnt[b[j] < L] * a[L]
      int val = c[a[L]];
      update(cnt1, sum1, a[L], 1, val);
      sumx += val;
      ll x = lower_bound(d + 1, d + n + 1, val) - d - 1;
      ll y = query_cnt(cnt2, x);
      ll z = query_sum(sum2, x);
      sum += y * val - z;
      sum += sumy - z - (R - y) * val;
    }
    while (L > q[i].x) {
      int val = c[a[L]];
      update(cnt1, sum1, a[L], -1, -val);
      sumx -= val;
      ll x = lower_bound(d + 1, d + n + 1, val) - d - 1;
      ll y = query_cnt(cnt2, x);
      ll z = query_sum(sum2, x);
      sum -= y * val - z;
      sum -= sumy - z - (R - y) * val;
      L--;
    }
    while (R < q[i].y) {
      ++R;
      int val = d[b[R]];
      update(cnt2, sum2, b[R], 1, val);
      sumy += val;
      ll x = lower_bound(c + 1, c + n + 1, val) - c - 1;
      ll y = query_cnt(cnt1, x);
      ll z = query_sum(sum1, x);
      sum += y * val - z;
      sum += sumx - z - (L - y) * val;
    }
    while (R > q[i].y) {
      int val = d[b[R]];
      update(cnt2, sum2, b[R], -1, -val);
      sumy -= val;
      ll x = lower_bound(c + 1, c + n + 1, val) - c - 1;
      ll y = query_cnt(cnt1, x);
      ll z = query_sum(sum1, x);
      sum -= y * val - z;
      sum -= sumx - z - (L - y) * val;
      R--;
    }
    ans[q[i].id] = sum;
  }
  rep(i, 1, m) cout << ans[i] << endl;
  return 0;
}